<!DOCTYPE html>
<html lang="zh-CN">
    <style>
        /* styles.css */
body {
    font-family: Arial, sans-serif;
}

.carousel {
    position: relative;
    max-width: 600px;
    margin: auto;
    overflow: hidden;
}

.slides {
    display: flex;
    transition: transform 0.5s ease;
}

.slide {
    min-width: 100%;
    box-sizing: border-box;
}

.slide img {
    width: 100%;
    vertical-align: middle;
}

.prev, .next {
    cursor: pointer;
    position: absolute;
    top: 50%;
    width: auto;
    padding: 16px;
    margin-top: -22px;
    color: white;
    font-weight: bold;
    font-size: 18px;
    transition: 0.6s ease;
    border-radius: 0 3px 3px 0;
    user-select: none;
}

.prev {
    left: 0;
}

.next {
    right: 0;
    border-radius: 3px 0 0 3px;
}

.prev:hover, .next:hover {
    background-color: rgba(0,0,0,0.8);
}

    </style>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图示例</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="carousel">
        <button class="prev" onclick="changeSlide(-1)">&#10094;</button>
        <div class="slides">
            <div class="slide"><img src="./image/1.jpg" alt="Image 1"></div>
            <div class="slide"><img src="./image/2.jpg" alt="Image 2"></div>
            <div class="slide"><img src="./image/3.jpg" alt="Image 3"></div>
            <div class="slide"><img src="./image/4.jpg" alt="Image 4"></div>
        </div>
        <button class="next" onclick="changeSlide(1)">&#10095;</button>
    </div> 
         
</body>
<script>
    /* styles.css */
// script.js
let slideIndex = 1;
showSlides(slideIndex);

function showSlides(n) {
    const slides = document.querySelectorAll(".slide");
    if (n > slides.length) {slideIndex = 1}    
    if (n < 1) {slideIndex = slides.length}
    for (let i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";  
    }
    slides[slideIndex - 1].style.display = "block";  
}

function changeSlide(n) {
    showSlides(slideIndex += n);
}


</script>
</html>
